package dao;

import dao.imp.ITitlesDao;
import entity.Job;
import entity.Titles;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.util.List;

/**
 * @program: personnel_management_system
 * @description: title dao
 * @author: Mr.Sun
 * @create: 2020-05-25 14:59
 **/

public class TitlesDao implements ITitlesDao {
    private JdbcTemplate template = new JdbcTemplate(MysqlConnUtil.getDataSource());

    public TitlesDao() throws Exception {
    }

    @Override
    public boolean insert(Titles titles) {
        String sql = "insert into titles(title_name,emp_id) values (?,?)";
        Integer count = template.update(sql,titles.getTitle_name(),titles.getEmp_id());
        return count == 1;
    }

    @Override
    public boolean update(Titles titles) {
        boolean res = false;
        String sql = "UPDATE titles SET title_name = ? ,emp_id =? WHERE t_id = ?";
        int count = template.update(sql,titles.getTitle_name(),titles.getEmp_id(),titles.getT_id());
        res = count == 1;
        return res;
    }

    @Override
    public boolean delete(Titles titles) {
        boolean res =false;
        String sql = "DELETE FROM titles WHERE t_id = ?";
        int count = template.update(sql,titles.getT_id());
        return count == 1;
    }

    @Override
    public List<Titles> selectAll() {
        String sql = "select * from titles";
        List<Titles> titles = template.query(sql, new BeanPropertyRowMapper<Titles>(Titles.class));
        return titles;
    }

    @Override
    public List<Titles> select(Titles titles) {
        return null;
    }
}
